package anc.tankshooter.ui;

import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;

import anc.tankshooter.GameActivity;
import anc.tankshooter.GameSetting;
import anc.tankshooter.controls.InputText;
import anc.tankshooter.ui.SceneManager.SceneType;

public class ResultScreen extends IScreen{
	
	private int mLevel;
	private int mNormalTank,mFastTank,mHeavyTank,mBossTank,mScore;
	private boolean mWinGame;
	private Sprite mBackground ;
	private TiledSprite mTitleDialog;
	private InputText mNameText;
	private TiledSprite mLevelText;
	private Text mNormalCounter,mFastCounter ,mHeavyCounter,mBossCounter,mScoreCounter;
	
	private TiledSprite mBtnReplay,mBtnSave,mBtnNext;
	private ResultDialogWatcher mWatcher;
	
	
	public ResultScreen(boolean isWin ,
						int level,
						int normalTank,int fastTank,int heavyTank,int bossTank,int scoreTank, 
						ResultDialogWatcher watcher){
		mWinGame=isWin;
		mWatcher=watcher;
		mLevel=level;
		mNormalTank=normalTank;
		mFastTank=fastTank;
		mHeavyTank=heavyTank;
		mBossTank = bossTank;
		mScore=scoreTank;
		
		mLevelText.setCurrentTileIndex(mLevel);
		attachChild(mBackground);
		attachChild(mTitleDialog); mTitleDialog.setCurrentTileIndex(mWinGame?4:1);
		
		attachChild(mLevelText);
		attachChild(mNormalCounter); mNormalCounter.setText(mNormalTank+"");
		attachChild(mFastCounter); mFastCounter.setText(mFastTank+"");
		attachChild(mHeavyCounter);mHeavyCounter.setText(mHeavyTank+"");
		attachChild(mBossCounter);mBossCounter.setText(mBossTank+"");
		attachChild(mScoreCounter);mScoreCounter.setText(mScore+"");
		
		attachChild(mBtnReplay); registerTouchArea(mBtnReplay);
		if(mWinGame) {
			
			if(GameActivity.mGlobalSetting.getCurrentLevel() < GameSetting.MAX_LEVEL) {
				mTitleDialog.setY(GameActivity.CAMERA_HEIGHT/2 - 300/2+20);
				attachChild(mBtnNext); registerTouchArea(mBtnNext);
			}else {
				attachChild(mNameText); registerTouchArea(mNameText);
				 mTitleDialog.setCurrentTileIndex(0);
				attachChild(mBtnSave); registerTouchArea(mBtnSave);
			}
		}else {
			attachChild(mNameText); registerTouchArea(mNameText);
			attachChild(mBtnSave); registerTouchArea(mBtnSave);
		}
		
		
		this.setBackgroundEnabled(false);
	}
	
	@Override
	protected void createScene() {
		float x = GameActivity.CAMERA_WITDH/2 - 390/2;
		float y = GameActivity.CAMERA_HEIGHT/2 - 300/2;
		
		mBackground = new Sprite(x,y,ResourcesManager.getInstance().mResultPanelTexture,  ResourcesManager.getInstance().mVertexManager);
		mTitleDialog=new TiledSprite(x,y, ResourcesManager.getInstance().mDialogTitlesTexture, ResourcesManager.getInstance().mVertexManager);
		
		mNameText=new InputText(x+100, y+50,"Player name", ResourcesManager.getInstance().mTextBoxTexture,
								ResourcesManager.getInstance().mWhiteFont20,8);
		mNameText.setSize(200,30);
		
		mLevelText = new TiledSprite(x+320, y+100, ResourcesManager.getInstance().mNumbersTexture,
				ResourcesManager.getInstance().mVertexManager);
		
		
		mNormalCounter = new Text(x+200, y+95, ResourcesManager.getInstance().mWhiteFont20,"0",4, ResourcesManager.getInstance().mVertexManager);
		mFastCounter = new Text(x+200, y+125, ResourcesManager.getInstance().mWhiteFont20,"0",4, ResourcesManager.getInstance().mVertexManager);
		mHeavyCounter = new Text(x+200, y+155, ResourcesManager.getInstance().mWhiteFont20,"0",4, ResourcesManager.getInstance().mVertexManager);
		mBossCounter = new Text(x+200, y+180, ResourcesManager.getInstance().mWhiteFont20,"0",4, ResourcesManager.getInstance().mVertexManager);
		mScoreCounter = new Text(x+200, y+210, ResourcesManager.getInstance().mWhiteFont20,"0",4, ResourcesManager.getInstance().mVertexManager);
		
		mBtnReplay = new TiledSprite(x+90, y+250,ResourcesManager.getInstance().mBtnTexture, mVertextManager){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP||pSceneTouchEvent.getAction()==TouchEvent.ACTION_OUTSIDE||
						pSceneTouchEvent.getAction()==TouchEvent.ACTION_CANCEL){
					mBtnReplay.setCurrentTileIndex(0);
					if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP)
						mWatcher.onPlayAgain();
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN) {
					mBtnReplay.setCurrentTileIndex(1);
				}
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		mBtnReplay.setCurrentTileIndex(0);
		mBtnSave = new TiledSprite(x+100+80*1+10, y+250,ResourcesManager.getInstance().mBtnTexture, mVertextManager){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP||pSceneTouchEvent.getAction()==TouchEvent.ACTION_OUTSIDE||
						pSceneTouchEvent.getAction()==TouchEvent.ACTION_CANCEL){
					mBtnSave.setCurrentTileIndex(2);
					if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP) {
						mWatcher.onSaveHighScore(mNameText.getText());
					}
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN) {
					mBtnSave.setCurrentTileIndex(3);
				}
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		mBtnSave.setCurrentTileIndex(2);
		
		mBtnNext = new TiledSprite(x+100+80*1+10*1, y+250,ResourcesManager.getInstance().mBtnTexture, mVertextManager){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP||pSceneTouchEvent.getAction()==TouchEvent.ACTION_OUTSIDE||
						pSceneTouchEvent.getAction()==TouchEvent.ACTION_CANCEL){
					mBtnNext.setCurrentTileIndex(4);
					if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP)
						mWatcher.onNextLevel();
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN) {
					mBtnNext.setCurrentTileIndex(5);
				}
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		mBtnNext.setCurrentTileIndex(4);
		
		
		
	}

	@Override
	public void onUpdateScreen(float second) {
	}

	@Override
	public boolean onBackKeyPressed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public SceneType getSceneType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void disposeScene() {
		
	}
		
	public static interface ResultDialogWatcher{
		public void onPlayAgain();
		public void onSaveHighScore(String name);
		public void onNextLevel();
	}
}
